package com.dormitory.dormitorymanagement.service.ipml;

import com.dormitory.dormitorymanagement.constant.Constant;
import com.dormitory.dormitorymanagement.mapper.AdminMapper;
import com.dormitory.dormitorymanagement.mapper.ClassLeaderMapper;
import com.dormitory.dormitorymanagement.mapper.StudentsMapper;
import com.dormitory.dormitorymanagement.pojo.*;
import com.dormitory.dormitorymanagement.pojo.dto.LoginDto;
import com.dormitory.dormitorymanagement.pojo.dto.*;
import com.dormitory.dormitorymanagement.pojo.vo.CommentsVo;
import com.dormitory.dormitorymanagement.pojo.vo.PostsVo;
import com.dormitory.dormitorymanagement.pojo.vo.RepliesVo;
import com.dormitory.dormitorymanagement.pojo.vo.StudentEvaluationsVo;
import com.dormitory.dormitorymanagement.service.ClassLeaderService;
import com.dormitory.dormitorymanagement.service.StudentsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component

public class StudentsServiceIpml implements StudentsService{

    @Autowired
    private StudentsMapper studentsMapper;

    @Autowired
    private ClassLeaderMapper classLeaderMapper;
    @Autowired
    private AdminMapper  adminMapper;

    /**
     * 登录
     * @param loginDto
     * @return
     */
    public Students login(LoginDto loginDto) {

        Students student = studentsMapper.findByStudentId(loginDto.getUsername());
        return student;


    }


    /**
     * 添加保修记录
     * @param dormRepair
     */

    public void insert(DormRepair dormRepair) {
        studentsMapper.insertImpair(dormRepair);
    }

    /**
     * 根据id查询学生
     * @param studentIds
     * @return
     */
    public List<Students> getStudentsByIds(List<String> studentIds) {
        return studentsMapper.findByStudentIds(studentIds);
    }

    /**
     * 查询所有学生
     * @return
     */
    public List<Students> getStudentsAll() {
        return studentsMapper.getStudentsAll();
    }

    /**
     * 查询最新公告
     * @param studentId
     * @param pageSize
     * @return
     */
    public List<Notice> getLatestNotice(String studentId, Integer pageSize) {
        //查询学生所在楼栋
        Students  students = studentsMapper.findByStudentId(studentId);
        //根据楼栋和最新条数查询最新公告
        List<Notice> notices = studentsMapper.getLatestNotice(students.getDormBuilding(), pageSize);


        return notices;
    }

    /**
     * 获取公告详情
     * @param noticeId
     * @return
     */
    public Notice getNoticeDetail(Integer noticeId) {
        return studentsMapper.getNoticeDetail(noticeId);
    }

    /**
     * 获取班级公告
     * @param studentId
     * @param pageSize
     * @return
     */
    public List<ClassNotice> getLatestClassNotice(String studentId, Integer pageSize) {
        //根据学号查询学生所在班级
        Students student = studentsMapper.findByStudentId(studentId);
        List<ClassNotice> classNotices = studentsMapper.getLatestClassNotice(student.getClassName(), pageSize);
        return classNotices;
    }

    /**
     * 获取详细班级通知
     * @param id
     * @return
     */
    public ClassNotice getClassNoticeDetail(Integer id) {
        return studentsMapper.getClassNoticeDetail(id);
    }

    /**
     * 获取课表
     * @param studentId
     * @return
     */
    public List<CourseSchedule> getCources(String studentId) {

        Students student = studentsMapper.findByStudentId(studentId);
        List<CourseSchedule> course = classLeaderMapper.findByClassName(student.getClassName());

        return course;


    }

    /**
     * 获取班干部
     * @param page
     * @param pageSize
     * @return
     */
    public PageBean<Students> getClassLeader(String studentId, Integer page, Integer pageSize) {
        PageBean<Students> pb = new PageBean<>();

        // 先查询学生信息，获取班级名称
        Students student = studentsMapper.findByStudentId(studentId);

        if (student == null) {
            // 处理学生不存在的情况
            pb.setItems(Collections.emptyList());
            pb.setTotal(0L);
            return pb;
        }

        // 开启分页查询
        PageHelper.startPage(page, pageSize);

        // 执行分页查询，获取班干部列表
        List<Students> as = studentsMapper.getClassLeader(student.getClassName());

        // 使用PageInfo包装查询结果，获取分页信息
        PageInfo<Students> pageInfo = new PageInfo<>(as);

        // 填充数据到PageBean
        pb.setItems(pageInfo.getList());
        pb.setTotal(pageInfo.getTotal());

        return pb;
    }

    /**
     * 获取宿管
     * @param studentId
     * @param page
     * @param pageSize
     * @return
     */
    public PageBean<DormitoryAdmin> getDormAdmin(String studentId, Integer page, Integer pageSize) {
        PageBean<DormitoryAdmin> pb = new PageBean<>();

        // 先查询学生信息，获取楼栋名称
        Students student = studentsMapper.findByStudentId(studentId);

        if (student == null) {
            // 处理学生不存在的情况
            pb.setItems(Collections.emptyList());
            pb.setTotal(0L);
            return pb;
        }

        // 开启分页查询
        PageHelper.startPage(page, pageSize);

        // 执行分页查询，获取班干部列表
        List<DormitoryAdmin> as = studentsMapper.getDormAdmin(student.getDormBuilding());

        // 使用PageInfo包装查询结果，获取分页信息
        PageInfo<DormitoryAdmin> pageInfo = new PageInfo<>(as);

        // 填充数据到PageBean
        pb.setItems(pageInfo.getList());
        pb.setTotal(pageInfo.getTotal());

        return pb;
    }

    /**
     * 评价
     * @param evaluationDto
     */
    public void submit(EvaluationDto evaluationDto) {
        StudentEvaluations evaluations = new StudentEvaluations();
        evaluations.setStudentId(evaluationDto.getStudentId());
        evaluations.setTargetId(evaluationDto.getTargetId());

        evaluations.setTargetRole(evaluationDto.getTargetType());
        evaluations.setContent(evaluationDto.getContent());
        evaluations.setScore(evaluationDto.getScore());
        evaluations.setAttitude(evaluationDto.getAttitude());
        evaluations.setCommunication(evaluationDto.getCommunication());
        evaluations.setAbility(evaluationDto.getAbility());
        evaluations.setStatus("已评价");
        evaluations.setCreateTime(LocalDateTime.now());
        Students student = studentsMapper.findByStudentId(evaluationDto.getStudentId());
        if(evaluationDto.getTargetType().equals("class")) {
            evaluations.setTargetPosition(student.getRole());
            evaluations.setStudentClass(student.getClassName());
        }else{
            evaluations.setTargetPosition(student.getDormBuilding() + "宿管");
            evaluations.setStudentClass(Constant.DormAdmin);
        }

        studentsMapper.submit(evaluations);
    }

    /**
     * 获取学生评价
     * @param targetId
     * @param targetType
     * @return
     */
    public StudentEvaluations getStudentEvaluations(String targetId, String targetType) {
        StudentEvaluations evaluation = studentsMapper.getStudentEvaluations(targetId, targetType);
        return null;
    }

    /**
     * 帖子提交
     * @param postsDto
     */
    public void postsSubmit(PostsDto postsDto) {
        Posts posts = new Posts();
        Students student = studentsMapper.findByStudentId(postsDto.getStudentId());
        posts.setStudentClass(student.getClassName());
        posts.setTitle(postsDto.getTitle());
        posts.setContent(postsDto.getContent());
        posts.setStudentId(postsDto.getStudentId());
        posts.setAuthor(postsDto.getAuthor());
        posts.setCreateTime(LocalDateTime.now());

        studentsMapper.postsSubmit(posts);

    }

    /**
     * 返回帖子列表
     * @param studentId
     * @return
     */
    public List<PostsVo> getPostsList(String studentId) {
        List<PostsVo> postsVoList = new ArrayList<>();
        Students student = studentsMapper.findByStudentId(studentId);
        //先查找关于班级的所有帖子
        List<Posts> posts = studentsMapper.getPostsList(student.getClassName());


        //遍历找到的每个帖子的id，寻找评论
        for (Posts post : posts) {
            PostsVo  postsVo = new PostsVo();
            postsVo.setId(post.getId());
            postsVo.setTitle(post.getTitle());
            postsVo.setContent(post.getContent());
            postsVo.setCreatTime(post.getCreateTime());
            postsVo.setAuthor(post.getAuthor());
            List<CommentsVo> commentsVoList = new ArrayList<>();
            List<Comments> comments = studentsMapper.getComments(post.getId());
            //遍历每个评论的id寻找评论的回复
            for (Comments comment : comments){
                CommentsVo commentsVo = new CommentsVo();
                commentsVo.setId(comment.getId());
                commentsVo.setAuthor(comment.getAuthor());
                commentsVo.setContent(comment.getContent());
                commentsVo.setCreateTime(comment.getCreateTime());

                List<RepliesVo> repliesVoList = studentsMapper.getReplies(comment.getId());
                commentsVo.setReplies(repliesVoList);
                commentsVoList.add(commentsVo);

            }

            postsVo.setComments(commentsVoList);
            postsVoList.add(postsVo);

        }

        return postsVoList;


    }

    /**
     * 添加评论
     * @param commentsDto
     */
    public void commentSubmit(CommentsDto commentsDto) {
        Comments comments = new Comments();
        comments.setStudentId(commentsDto.getStudentId());
        comments.setAuthor(commentsDto.getAuthor());
        comments.setContent(commentsDto.getContent());
        comments.setCreateTime(LocalDateTime.now());
        comments.setPostId(commentsDto.getPostId());

        studentsMapper.commentSubmit(comments);

    }

    /**
     * 回复
     * @param replyDto
     */
    public void replySubmit(ReplyDto replyDto) {
        Replies replies = new Replies();
        replies.setStudentId(replyDto.getStudentId());
        replies.setAuthor(replyDto.getAuthor());
        replies.setContent(replyDto.getContent());
        replies.setCreateTime(LocalDateTime.now());
        replies.setCommentId(replyDto.getCommentId());

        studentsMapper.replySubmit(replies);

    }

    /**
     * 获取我的评论
     * @param studentId
     * @param page
     * @param pageSize
     * @return
     */
    public PageBean<StudentEvaluationsVo> getStudentMyEvaluations(String studentId, Integer page, Integer pageSize) {
        PageBean<StudentEvaluationsVo> pb = new PageBean<>();

        // 开启分页查询
        PageHelper.startPage(page, pageSize);

        // 执行分页查询
        List<StudentEvaluations> as = studentsMapper.getStudentMyEvaluations(studentId);
        List<StudentEvaluationsVo> studentEvaluationsVoList = new ArrayList<>();
        if (as.size() > 0) {
            for (StudentEvaluations evaluations : as) {
                StudentEvaluationsVo studentEvaluationsVo = new StudentEvaluationsVo();
                BeanUtils.copyProperties(evaluations, studentEvaluationsVo);
                if (evaluations.getTargetRole().equals("class")) {
                    Students student = studentsMapper.findByStudentId(evaluations.getTargetId());
                    studentEvaluationsVo.setTargetName(student.getName());
                } else {
                    DormitoryAdmin dormitoryAdmin = adminMapper.findById(evaluations.getTargetId());
                    studentEvaluationsVo.setTargetName(dormitoryAdmin.getName());
                }
                studentEvaluationsVoList.add(studentEvaluationsVo);
            }
        }
        // 使用PageInfo包装查询结果，获取分页信息
        PageInfo<StudentEvaluationsVo> pageInfo = new PageInfo<>(studentEvaluationsVoList);

        // 填充数据到PageBean
        pb.setItems(pageInfo.getList());
        pb.setTotal(pageInfo.getTotal());

        return pb;
    }

    @Override
    public void studentRedister(UserDto userDto) {
        Students students = new Students();
        students.setStudentId(userDto.getStudentId());
        students.setName(userDto.getName());
        students.setDormBuilding(userDto.getBuilding());
        students.setDormRoom(userDto.getDormitoryNumber());
        students.setPassword(userDto.getPassword());
        students.setUsername(userDto.getUsername());
        students.setRole("学生");
        students.setCreateTime(LocalDateTime.now());
        students.setUpdateTime(LocalDateTime.now());

        studentsMapper.insert(students);

    }

    @Override
    public void classLeaderRedister(UserDto userDto) {
        Students students = new Students();
        students.setStudentId(userDto.getStudentId());
        students.setName(userDto.getName());
        students.setPassword(userDto.getPassword());
        students.setUsername(userDto.getUsername());
        students.setRole(userDto.getRole());
        students.setClassName(userDto.getClassName());
        students.setCreateTime(LocalDateTime.now());
        students.setUpdateTime(LocalDateTime.now());

        studentsMapper.insertClassLeader(students);
    }


}
